package cn.szzsi.common.model;

import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.base.BaseOrderItem;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * Bean类 - 订单项
 */
public class OrderItem extends BaseOrderItem<OrderItem> {

	private static final long serialVersionUID = 8891675666120549939L;
	
	public static final OrderItem dao = new OrderItem().dao();
	
	// 产品
	public Product getProduct(){
		return Product.dao.findById(getStr("product_id"));		
	}
	//产品(返回需要的字段)
	public Product getExceptProduct(){
		return Product.dao.findFirst("select id,name,productImageListStore from mall_product where id=?",getStr("product_id"));
	}

	// 产品List
	public List<Product> getProductItemList(){
		String sql = "select * from mall_product where id = ?";
		System.out.println(getStr("product_id"));
		return Product.dao.find(sql,getStr("product_id"));		
	}
	
	// 订单
	public Orders getOrder() {
		return Orders.dao.findById(getStr("order_id"));
	}

	//根据订单id和产品id获取对象
	public OrderItem getByOrderIdAndProductId(String orderId, String productId){
		return dao.findFirst("select * from mall_order_item where order_id=? and product_id=?",orderId,productId);
	}

	/**
	 *
	 * @param orderId
	 * @param productSn
	 * @return
	 */
	public OrderItem getByOrderIdAndProductSn(String orderId, String productSn){
		return dao.findFirst("select * from mall_order_item where order_id=? and productSn=?",orderId,productSn);
	}

	//根据订单id查询
	public List<OrderItem> getByOrderIdAndStatus(String orderId, int evaluationStatus){
		return dao.find("select * from mall_order_item where order_id=? and isEvaluate=?",orderId,evaluationStatus);
	}

	/**
	 * 根据规格id查询规格名称(前端模板调用)
	 */
	public String getSpecificationvaluesAll(){
		String product_id = this.getStr("product_id");
		Product product=Product.dao.findById(product_id);
		String valuenames=product.getSpecificationValues();
		return valuenames;
	}

	
	//重写save
	public boolean save(OrderItem orderItem){
		orderItem.set("id", CommonUtil.getUUID());
		orderItem.set("createDate", new Date());
		return orderItem.save();
	}
}
